/**
 * 初始化部门详情对话框
 */
var ConsultantInfoDlg = {
    consultantInfoData: {},
    zTreeInstance: null,
    validateFields: {
        customerName: {
            validators: {
                notEmpty: {
                    message: '客户姓名不能为空'
                }
            }
        },
        customerPhone: {
            validators: {
                notEmpty: {
                    message: '电话不能为空'
                }
            }
        },
        amountOfPayment: {
            validators: {
                regexp: {
                    /* 只需加此键值对，包含正则表达式，和提示 */
                    regexp: /^[0-9]{1}\d{0,6}(\.\d{1,2})?$/,
                    message: '整数位不超过7位，小数点后不超过2位小数，请重新输入...！'
                },
                notEmpty: {
                    message: '付款金额不能为空'
                }
            }
        },
        customerAge: {
            validators: {
                notEmpty: {
                    message: '年龄不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
ConsultantInfoDlg.clearData = function () {
    this.consultantInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsultantInfoDlg.set = function (key, val) {
    this.consultantInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ConsultantInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ConsultantInfoDlg.close = function () {
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}


/**
 * 显示部门选择的树
 *
 * @returns
 */
ConsultantInfoDlg.showConsultantSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentConsultantMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
ConsultantInfoDlg.hideConsultantSelectTree = function () {
    $("#parentConsultantMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

ConsultantInfoDlg.choiceCheckoutMethods = function () {
    var reservationsId = $('#reservationsId').val();
    var customerId = $('#customerId').val();
    var operationTime = $('#operationTime').val();
    var rco = reservationsId + "," + customerId + "," + operationTime;
    var index = layer.open({
        type: 2,
        title: '选择结账方式',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/recharge/to_choiceCheckoutMethods/' + rco
    });
    this.layerIndex = index;
}

/**
 * 收集数据
 */
ConsultantInfoDlg.collectData = function () {
    this.set('id').set('supplementOfPayment').set('reservationsId').set('totleFoldingPay').set('diagnosisContent').set('amountOfPayment').set('operationTime');
}

/**
 * 验证数据是否为空
 */
ConsultantInfoDlg.validate = function () {
    $('#consultantInfoForm').data("bootstrapValidator").resetForm();
    $('#consultantInfoForm').bootstrapValidator('validate');
    return $("#consultantInfoForm").data('bootstrapValidator').isValid();
}


ConsultantInfoDlg.addPayment = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var totleFoldingPay = $('#totleFoldingPay').val();
    var amountOfPayment = $('#amountOfPayment').val();
    if (parseFloat(totleFoldingPay) < parseFloat(amountOfPayment)) {
        alert("付款金额要小于应实付金额！");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consultant/addPayment", function (data) {
        Feng.success("付款成功!");
        window.parent.parent.Consultant.table.refresh();
        $('#cancel', parent.document).click();
    }, function (data) {
        Feng.error("付款失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consultantInfoData);
    ajax.start();
}


ConsultantInfoDlg.addSupplement = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    var accountArrears = $('#accountArrears').val();//欠款金额
    var supplementOfPayment = $('#supplementOfPayment').val();//补签款金额
    var aa=accountArrears.substring(0,accountArrears.length-2);
    var bb=supplementOfPayment;
    if (+aa < +bb) {
        alert("欠款金额要大于等于补签款金额！");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consultant/addSupplement", function (data) {
        Feng.success("补欠款成功!");
        window.parent.Recharge.table.refresh();
        var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        parent.layer.close(index);
    }, function (data) {
        Feng.error("补欠款失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consultantInfoData);
    ajax.start();
}


/**
 * 提交修改
 */
ConsultantInfoDlg.diagnosis = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if ($('#diagnosisContent').val() == "" || $('#diagnosisContent').val() == null) {
        alert("诊断不能为空");
        return;
    }
    if ($('#diagnosisContent').length > 30000) {
        alert("诊断内容小于3万");
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/consultant/diagnosis", function (data) {
        Feng.success("看诊成功!");
        window.parent.Consultant.table.refresh();
        ConsultantInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.consultantInfoData);
    ajax.start();
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentConsultantMenu" || $(
            event.target).parents("#parentConsultantMenu").length > 0)) {
        ConsultantInfoDlg.hideConsultantSelectTree();
    }
}

$(function () {
    Feng.initValidator("consultantInfoForm", ConsultantInfoDlg.validateFields);

});
